#!/usr/bin/perl -w
use strict;
use DBI;
use Statistics::Descriptive;

# Jason Weirather
# this will load a bed file into the database so it will be usable by other programs.
#### we need to bring in quite a bit of information to properly label any thing we load

my $species = 'hg18';
my $original_source = 'hg18_combined_H3k4me1.bed';
my $feature_name = 'H3k4me1';
my $cell_type = 'any';
my $note = 'combined all cell type data from ENCODE';
my $replicate_number = 1;


my $group_label = shift @ARGV;
my $experiment_name = shift @ARGV;
my @lines_to_add;
chomp(my @lines = <STDIN>);
my $total_width = 0;
my $widthstat = Statistics::Descriptive::Full->new();
my $heightstat = Statistics::Descriptive::Full->new();
foreach my $line (@lines) {
  my @fields = split(/\t/,$line);
  my $midpoint = int (($fields[2] + $fields[1])/2);
  push @lines_to_add, "$fields[0]\t$fields[1]\t$fields[2]\t$fields[3]\t$midpoint";
  $widthstat->add_data($fields[2]-$fields[1]);
  $heightstat->add_data($fields[3]);
}
print $widthstat->max() . " " . $heightstat->max() . "\n";
print $widthstat->mean() . " " . $heightstat->mean() . "\n";
print $widthstat->standard_deviation() . " " . $heightstat->standard_deviation() . "\n";

my $zdatabase = "zebraphish";
my $platform = "mysql";
my $user = 'x';
my $zdbh = DBI->connect("dbi:$platform:$zdatabase",$user,'');
my $zquery = "SELECT db_id FROM feature_overlay_info WHERE species = '$species' AND original_source = '$original_source' AND feature_name = '$feature_name' AND cell_type = '$cell_type' AND replicate_number = $replicate_number ORDER BY db_id DESC LIMIT 1";
print "$zquery\n";
my $zst = $zdbh->prepare($zquery) or die "preparing query failed: $DBI::errstr";
$zst->execute() or die "The execution of query field: $DBI::errstr";
while(my $row = $zst->fetchrow_hashref()) {
  die "already did this one.\n";
}

$zquery = "INSERT INTO feature_overlay_info (species,original_source,feature_name,cell_type,replicate_number,note,average_peak_height,average_peak_width,stddev_peak_height,stddev_peak_width) VALUES ('$species','$original_source','$feature_name','$cell_type','$replicate_number','$note'," . $heightstat->mean() . "," . $widthstat->mean() . "," . $heightstat->standard_deviation() . "," . $widthstat->standard_deviation() . ")";
print "$zquery\n";
$zst = $zdbh->prepare($zquery) or die "preparing query failed: $DBI::errstr";
$zst->execute() or die "The execution of query field: $DBI::errstr";
$zquery = "SELECT db_id FROM feature_overlay_info ORDER BY db_id DESC LIMIT 1";
print "$zquery\n";
$zst = $zdbh->prepare($zquery) or die "preparing query failed: $DBI::errstr";
$zst->execute() or die "The execution of query field: $DBI::errstr";
my $db_id;
while(my $row = $zst->fetchrow_hashref()) {
  $db_id = $row->{'db_id'};
}
print "$db_id\n";
foreach my $line (@lines_to_add) {
  my ($chromosome,$start,$finish,$score,$midpoint) = split(/\t/,$line);
  $zquery = "INSERT INTO feature_overlay (feature_overlay_info_id,chromosome,start,finish,score,midpoint) VALUES ('$db_id','$chromosome','$start','$finish','$score','$midpoint')";
  print "$zquery\n";
  $zst = $zdbh->prepare($zquery) or die "preparing query failed: $DBI::errstr";
  $zst->execute() or die "The execution of query field: $DBI::errstr";
}
